//
//  UIImageView+ZMExtension.swift
//  HaiLuo
//
//  Created by Rowling on 2020/6/12.
//  Copyright © 2020 Rowling. All rights reserved.
//

import UIKit
import Kingfisher
extension ZM where Base : UIImageView {
    func setImage(urlStr : String , placeholder : String? = "def_img",options : KingfisherOptionsInfo? = nil, completionHandler : ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) {
        if urlStr.count == 0 ,let placeholder = placeholder{
            base.image = UIImage.init(named: placeholder)
            if let completionHandler = completionHandler {
                completionHandler(.failure(KingfisherError.imageSettingError(reason: .emptySource)))
            }
            return
        }
        base.kf.setImage(with: URL.init(string: urlStr), placeholder: placeholder != nil ? UIImage.init(named: placeholder ?? "") : nil ,options:options, completionHandler: completionHandler)
    }
    
    func createCode(str: String, centerImg: UIImage?) {
        // 创建二维码滤镜
        let filter = CIFilter(name: "CIQRCodeGenerator")
        // 恢复滤镜默认设置
        filter?.setDefaults()
        // 设置滤镜输入数据
        let data = str.data(using: String.Encoding.utf8)
        filter?.setValue(data, forKey: "inputMessage")
        // 设置二维码的纠错率
        filter?.setValue("M", forKey: "inputCorrectionLevel")
        // 从二维码滤镜里面, 获取结果图片
        var image = filter?.outputImage
        // 生成一个高清图片
        let transform = CGAffineTransform.init(scaleX: 20, y: 20)
        image = image?.transformed(by: transform)
        // 图片处理
        var resultImage = UIImage(ciImage: image!)
        // 设置二维码中心显示的小图标
        let center = centerImg
        resultImage = getClearImage(sourceImage: resultImage, center: center)
        // 显示图片
        base.image = resultImage
    }
    
    func getClearImage(sourceImage: UIImage, center: UIImage?) -> UIImage {
        let size = sourceImage.size
        // 开启图形上下文
        let sacle = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(size, true, 0)
//        UIGraphicsBeginImageContext(size)
        // 绘制大图片
        sourceImage.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        if let center = center {
            // 绘制二维码中心小图片
            let width: CGFloat = 80 * sacle
            let height: CGFloat = 80 * sacle
            let x: CGFloat = (size.width - width) * 0.5
            let y: CGFloat = (size.height - height) * 0.5
        
            let theRect = CGRect(x: x - 3 * sacle, y: y - 3 * sacle, width: width + 6 * sacle, height: height + 6 * sacle)
            let path = UIBezierPath.init(roundedRect: theRect, cornerRadius: 4 * sacle) //init(rect: theRect)//UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 40, height: 40))
            
            path.close()
            UIColor.white.setFill()
            path.stroke()
            path.fill()
            center.draw(in: CGRect(x: x, y: y, width: width, height: height))
        }

        // 取出结果图片
        let resultImage = UIGraphicsGetImageFromCurrentImageContext()
        // 关闭上下文
        UIGraphicsEndImageContext()
        return resultImage!
    }

}
